package cn.iocoder.yudao.module.exam.controller.admin.paperquestion;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.exam.controller.admin.paperquestion.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.paperquestion.PaperQuestionDO;
import cn.iocoder.yudao.module.exam.service.paperquestion.PaperQuestionService;

@Tag(name = "管理后台 - 试卷试题")
@RestController
@RequestMapping("/exam/paper-question")
@Validated
public class PaperQuestionController {

    @Resource
    private PaperQuestionService paperQuestionService;

    @PostMapping("/create")
    @Operation(summary = "创建试卷试题")
    @PreAuthorize("@ss.hasPermission('exam:paper-question:create')")
    public CommonResult<Long> createPaperQuestion(@Valid @RequestBody PaperQuestionSaveReqVO createReqVO) {
        return success(paperQuestionService.createPaperQuestion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新试卷试题")
    @PreAuthorize("@ss.hasPermission('exam:paper-question:update')")
    public CommonResult<Boolean> updatePaperQuestion(@Valid @RequestBody PaperQuestionSaveReqVO updateReqVO) {
        paperQuestionService.updatePaperQuestion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除试卷试题")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('exam:paper-question:delete')")
    public CommonResult<Boolean> deletePaperQuestion(@RequestParam("id") Long id) {
        paperQuestionService.deletePaperQuestion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得试卷试题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('exam:paper-question:query')")
    public CommonResult<PaperQuestionRespVO> getPaperQuestion(@RequestParam("id") Long id) {
        PaperQuestionDO paperQuestion = paperQuestionService.getPaperQuestion(id);
        return success(BeanUtils.toBean(paperQuestion, PaperQuestionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得试卷试题分页")
    @PreAuthorize("@ss.hasPermission('exam:paper-question:query')")
    public CommonResult<PageResult<PaperQuestionRespVO>> getPaperQuestionPage(@Valid PaperQuestionPageReqVO pageReqVO) {
        PageResult<PaperQuestionDO> pageResult = paperQuestionService.getPaperQuestionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PaperQuestionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出试卷试题 Excel")
    @PreAuthorize("@ss.hasPermission('exam:paper-question:export')")
    @OperateLog(type = EXPORT)
    public void exportPaperQuestionExcel(@Valid PaperQuestionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PaperQuestionDO> list = paperQuestionService.getPaperQuestionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "试卷试题.xls", "数据", PaperQuestionRespVO.class,
                        BeanUtils.toBean(list, PaperQuestionRespVO.class));
    }

}